package com.yujie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yujie.domain.Employee;
import com.yujie.domain.dto.EmployeeDTO;
import com.yujie.domain.query.EmployeeQuery;
import com.yujie.exception.BusinessException;
import com.yujie.exception.EmployeeErrorCodeEnum;
import com.yujie.service.EmployeeService;
import com.yujie.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.web.multipart.MultipartFile;

/**
 * (Employee)表控制层
 *
 * @author wangyujie
 * @since 2023-08-14 20:12:24
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {

    /**
     * 服务对象
     */
    @Resource
    private EmployeeService employeeService;

    /**
     * 分页查询所有数据
     */
    @PostMapping("/list")
    public R page(@RequestBody EmployeeQuery query) throws Exception {
        Page<Employee> page = new Page();
        page.setCurrent(query.getCurrent());
        page.setSize(query.getSize());
        Page<EmployeeDTO> list = this.employeeService.listPage(page, query);
        return R.ok().setData(list);
    }


    /**
     * 新增数据
     */
    @PostMapping("/save")
    public R save(@RequestBody EmployeeQuery query) {
        return R.ok().setData(this.employeeService.saveEmployee(query));
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    public R updateById(@RequestBody EmployeeQuery query) {
        return R.ok().setData(this.employeeService.updateEmployee(query));
    }

    /**
     * 单条/批量删除数据
     */
    @PostMapping("/delete")
    public R delete(@RequestBody List<Long> id) {
        return R.ok().setData(this.employeeService.removeByIds(id));
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/importEmp")
    @ResponseBody
    public R importEmployeeFromExcel(@RequestParam("file") MultipartFile file) throws Exception {
        employeeService.importEmployeeFromExcel(file);
        return R.ok();
    }

    /**
     * 导出
     * @param query
     * @return
     */
    @PostMapping(value = "/exportEmp")
    public void exportExcel(@RequestBody EmployeeQuery query) {
        Page<Employee> page = new Page();
        page.setCurrent(query.getCurrent());
        page.setSize(query.getSize());
        employeeService.exportEmpExcel(page,query);
    }

    /**
     * 下载模版
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/templateDownload")
    public void templateDownload() throws Exception {
        employeeService.exportTemplateExcel();
    }
}

